#!/usr/bin/perl 

use Getopt::Long;

my $opt_num = scalar(@ARGV);
my $verdi_top = "";
my $verdi_opt = "";
GetOptions(
  'cmp_dir=s'   => \$cmp_dir,
  'top=s'       => \$verdi_top,
  'verdi_opt=s' => \$verdi_opt,
) or usage();
if($opt_num == 0) {
  usage();
}

my @verdi_filelist = &find_valid_filelist($cmp_dir);
my $verdi_cmd = "verdi -sverilog -ntb_opts uvm -usevcs";
if(scalar(@verdi_filelist) == 0) {
  print "ERROR: Can't find valid filelist in $cmp_dir!!!\n";
  exit(1);
} else {
  foreach my $valid_file (@verdi_filelist) {
    $verdi_cmd .= " -f $cmp_dir\/$valid_file";
  }
}
if($verdi_top ne "") {
  $verdi_cmd .= " -top $verdi_top";
}
if($verdi_opt ne "") {
  $verdi_cmd .=  " ".$verdi_opt." ";
}
$verdi_cmd .= " &";
my $result = system($verdi_cmd);
if($result != 0) {
  print ("ERROR: Open Verdi FAILED!!!\n");
  exit(1);
}
#----------------------------------------------------------
#-                   Sub Routine                          -
#----------------------------------------------------------
sub find_valid_filelist {
  my $cur_dir = shift @_;
  if(!opendir DH, $cur_dir) {
    print "Can't open $cur_dir: $!\n";
    exit(1);
  }
  my @allfiles = grep { $_ =~ /\.lst$/ } readdir DH;
  return @allfiles;
}

sub usage() {
    print STDERR << "EOF";
This program is used to resolve dependency at any hierarchy of the IP where 
there is a block_dependency.cfg file.

usage: $0 [-h] {-cmp_dir <directory> | -top <top_name> | -verdi_opt <verdi options>} 

    -h                         : this (help) message
    -cmp_dir   <directory>     : Indicate compile directory
    -top       <top_name>      : Indicate the top module name
    -verdi_opt <verdi options> : Verdi options

EOF
    exit;
}



